import {
  loginWithCode,
  getToken,
  getCatalog,
  addProduct
} from "../../../api.js"
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsType: [],
    price_type: 0,
    channel: [{
      name: '品牌专柜',
      id: '1',
      choice: false
    },
    {
      name: '二手店',
      id: '2',
      choice: false
    },
    {
      name: '个人代购',
      id: '3',
      choice: false
    },
    {
      name: '礼品',
      id: '4',
      choice: false
    }
    ],
    alert: true,
    brand: '选择品牌',
    token_pic: '',
    form: {
      category_id: '',
      brand_id: '',
      brand_other: '',
      description: '',
      price_mode: '',
      price: '',
      pic_file: [],
      origin_price: '',
      source: '',
      remark: ''
    },
    radio_type: true,
    showImages: [], //显示图片
    count: 9,
    token_pic: '',
    shouru: '0',
    img_leng:3
  },
  onLoad: function (options) {
    var that = this
    getCatalog(
      getApp().globalData.businessId
    ).then(res => {
      if (res.data.code == 666) {
        console.log(res.data.data)
        var lis = res.data.data[1].children
        for (var i = 0; i < lis.length; i++) {
          if (i == 0) {
            lis[i].choice = true
          } else {
            lis[i].choice = false
          }
        }
        console.log(lis)
        that.setData({
          'form.category_id': lis[0].id,
          goodsType: lis
        })
      }
    })
  },
  submit(e) {
    var text = e.detail.value
    var that = this
    console.log(that.data.form)
    if (!text.description) {
      wx.showToast({
        title: '请填写商品描述',
        icon: 'none'
      })
      return
    } else if (that.data.form.pic_file.length == 0) {
      wx.showToast({
        title: '请至少上传一张图片',
        icon: 'none'
      })
      return
    } else if (that.data.brand == '选择品牌') {
      wx.showToast({
        title: '请选择品牌',
        icon: 'none'
      })
      return
    } else if (that.data.price_type == 0 && !text.price_one) {
      wx.showToast({
        title: '请填写到手价格',
        icon: 'none'
      })
      return
    }
    that.data.form.description = text.description
    that.data.form.origin_price = text.origin_price
    that.data.form.remark = text.remark
    if (that.data.price_type == 0) {
      that.data.form.price_mode = 1
      that.data.form.price = text.price_one
    }
    if (that.data.price_type == 1) {
      that.data.form.price_mode = 2
      that.data.form.price = text.price_two
    }
    console.log(that.data.form)
    that.setData({
      alert: false
    })
  },
  submit_alert(e) {
    console.log(e)
    var that = this
    var froms = that.data.form
    if (!this.data.radio_type) {
      wx.showToast({
        title: '请阅读平台卖家须知',
        icon: 'none'
      })
      return
    } else {
      addProduct(
        getApp().globalData.Store.token,
        { ...froms }
      ).then(res => {
        console.log(res)
        if (res.data.code == 666) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success'
          })
          this.setData({
            alert: true
          })
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/consignment/transfer/transfer?id=' + res.data.data.id,
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    }

  },
  alert_close(e) {
    this.setData({
      alert: true
    })
  },
  brandBtn(e) {
    wx.navigateTo({
      url: '/pages/consignment/brandChoice/brandChoice',
    })
  },
  type_tab(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    if (that.data.goodsType[index].choice) {
      return
    } else {
      for (var i = 0; i < that.data.goodsType.length; i++) {
        that.data.goodsType[i].choice = false
      }
      that.data.goodsType[index].choice = true
      that.setData({
        'form.category_id': that.data.goodsType[index].id,
        goodsType: that.data.goodsType
      })
    }
  },
  price_type_btn(e) {
    var type = e.currentTarget.dataset.type
    var that = this
    if (type == that.data.price_type) {
      return
    } else {
      that.setData({
        price_type: type
      })
    }
  },
  channel_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    if (that.data.channel[index].choice) {
      that.data.channel[index].choice = false
      that.setData({
        'form.source': '',
        channel: that.data.channel
      })
    } else {
      for (var i = 0; i < that.data.channel.length; i++) {
        that.data.channel[i].choice = false
      }
      that.data.channel[index].choice = true
      that.setData({
        'form.source': that.data.channel[index].name,
        channel: that.data.channel
      })
    }
  },
  alert_radio(e) {
    var that = this
    that.setData({
      radio_type: !that.data.radio_type
    })
  },
  //选择图片
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: that.data.count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.concat(tempFilePaths)
        if (15 - tempFilePaths.length > 9) {
          that.setData({
            count: 9
          })
        } else {
          that.setData({
            count: 15 - tempFilePaths.length
          })
        }
        console.log(showImages.length)
        if (showImages.length > 1){
          that.data.img_leng = 1
        }else{
          that.data.img_leng = 3 - showImages.length
        }
        that.setData({
          showImages: showImages,
          img_leng: that.data.img_leng
        })
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, 20)
        })
      }
    })
  },
  //图片上传到七牛
  uplod: function (obj, num) {
    var that = this
    var showImages = that.data.showImages
    for (var i = 0; i < obj.length; i++) {
      var timestamp = Date.parse(new Date()) + [i]
      var type = obj[i].substring(obj[i].lastIndexOf("."), obj[i].length);
      var keyy = timestamp + type
      wx.uploadFile({
        url: 'https://upload.qiniup.com',
        filePath: obj[i],
        name: 'file', //文件的key 
        header: {
          "Content-Type": "multipart/form-data"
        },
        formData: {
          token: that.data.token_pic,
          key: keyy
        }, //传递参数
        success: function (res) {
          var data = JSON.parse(res.data)
          var singleImg = {};
          if (that.data.isEdit) {
            singleImg.id = '';
          }
          if (num != 20) {
            that.data.form.pic_file[num].pic_url = data.key
          } else {
            var image = that.data.form.pic_file;
            var min;
            singleImg.pic_url = data.key
            image.push(singleImg)
            for (var i = 0; i < image.length; i++) {
              for (var j = i; j < image.length; j++) {
                if (parseInt(image[i].pic_url) > parseInt(image[j].pic_url)) {
                  min = image[j].pic_url;
                  image[j].pic_url = image[i].pic_url;
                  image[i].pic_url = min;
                }
              }
            }
            console.log(image)
          }
          that.setData({
            form: that.data.form
          })
        }
      })
    }
  },
  //替换img
  changeImg: function (e) {
    console.log(e.currentTarget.dataset.key)
    var inde = e.currentTarget.dataset.key
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        var showImages = that.data.showImages.splice(inde, 1, tempFilePaths[0])
        that.setData({
          showImages: that.data.showImages
        })
        console.log(that.data.showImages)
        getToken().then(res => {
          that.setData({
            token_pic: res.data.data.token,
          })
          that.uplod(tempFilePaths, inde)
        })
      }
    })
  },
  //图片删除
  img_delete(e) {
    var index = e.currentTarget.dataset.index
    var node = this.data.showImages
    this.data.form.pic_file.splice(index, 1)
    node.splice(index, 1)
    var that = this
    var that_count = 0
    console.log(that.data.showImages.length)
    if (15 - that.data.showImages.length > 9) {
      that_count = 9
    } else {
      that_count = 15 - that.data.showImages.length
    }
    if (that.data.showImages.length > 1) {
      that.data.img_leng = 1
    } else {
      that.data.img_leng = 3 - that.data.showImages.length
    }
    this.setData({
      showImages: this.data.showImages,
      count: that_count,
      img_leng: that.data.img_leng
    })
  },
  //图片查看
  img_look(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.showImages[index], // 当前显示图片的http链接
      urls: that.data.showImages // 需要预览的图片http链接列表
    })
  },
  qiwangs(e) {
    var num = (e.detail.value * 0.8).toFixed(2)
    this.setData({
      shouru: num
    })
  },
  onShow: function () {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
  gotoprotocol(){
    wx.navigateTo({
      url: '/pages/consignment/protocol/protocol',
    })
  },
  gokefu(){
    wx.navigateTo({
      url: '/pages/myList/message/message?type=4',
    })
  }
})